/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/FileCreator.h"


namespace alliance
{
const int Values_States[24][36]
{
//         0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 35 35
    //    A  B  C  D  E  F  G  H	I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  1  . " " (  )  :  ;  _ "-" ,
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//0*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//2*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//3*
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0, 0, 0},//4*
    { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,-1, 5,-1,-1,-1,-1, 6, 6,-1},//5*
    { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,-1,-1, 8,-1, 6,-1, 5},//6*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 7,-1,-1, 8,-1,-1,-1, 5},//7*
    { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,-1,-1, 8,-1,-1,-1,-1, 9, 9,-1},//8*
    { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,-1,10,-1,-1,-1,-1, 9, 9,-1},//9*
    {11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,-1,-1,10,-1,-1,-1,-1,11,11,-1},//10*
    {11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,16,13,-1,15,11,11,-1},//11*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,12,16,13,-1,15,-1,-1,-1},//12*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,13,-1,-1,-1,14,-1,-1,-1},//13*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//14**
    { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,-1,-1,15,-1,-1,-1,-1,-1,-1,-1},//15*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,17,-1,16,-1,-1,-1,-1,-1,-1,-1},//16*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,17,-1,18,-1,-1,-1,-1,-1,-1,-1},//17*
    {19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,18,-1,-1,-1,-1,19,19,-1},//18*
    {19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,20,-1,-1,-1,-1,19,19,-1},//19*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,21,-1,20,-1,-1,-1,-1,-1,-1,-1},//20*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,21,-1,22,-1,23,-1,-1,-1,-1,-1},//21*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,22,-1,23,-1,-1,-1,-1,-1},//22*
    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,23,-1,13,-1,15,-1,-1,-1}//23*
};




FileCreator::FileCreator(const wxString& title,char FileNameIn[],char FileNameOut[],int TypeFile):wxDialog(NULL,wxID_ANY,title,wxDefaultPosition,wxSize(330,430))
{

    Connect(ID_OK2,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(FileCreator::OnOk));
    Connect(ID_CANCEL2,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(FileCreator::OnCancel));
    Errors=false;
    FileOutFC=FileNameOut;
    TF=TypeFile;
    AnalyzerFile(FileNameIn);


    if(TypeFile)
    {
        ShowInterfaceValuesFileIOC();
    }

    else
    {
        ShowInterfaceValuesFilePAT();
    }


}



void FileCreator::OnOk(wxCommandEvent& event )
{

    if(TF)
    {
        CreatFileIOC(FileOutFC);
    }

    else
    {
        CreatFilePAT(FileOutFC);
    }

    EndModal(wxID_OK);
}


string FileCreator::ConvertionBinary(int num,int limit)
{
    string binary = "";
    int mask = 1;
    for(int i = 0; i <limit; i++)
    {
        if((mask&num) >= 1)
            binary = "1"+binary;
        else
            binary = "0"+binary;
        mask<<=1;
    }

    return binary;
}


bool FileCreator::GetErrorFileCreation()
{
    return Errors;
}


void FileCreator::CreatFilePAT(char FileNameOutPAT[])
{

    unsigned int i,j,k,l;
    int firstIte=1;
    unsigned int Max=0;
    int NodoMax=0;
    wxString St=StartTime->GetValue();
    wxString It=IncreTime->GetValue();
    wxString Ut=UnitTime->GetValue();
    wxString Re=Repetition->GetValue();

    string Stime=string(St.mb_str());
    string Itime=string(It.mb_str());
    string UTime=string(Ut.mb_str());
    unsigned int NumRepetition=wxAtoi(Re);

    //archivo de salida
    ofstream write_file(FileNameOutPAT);


    for(i=0,j=0; i<v.size(); i++)
    {

        write_file<<v[i].ModeInOut<<"\t\t"<<v[i].NameInOut<<" ";

        if(v[i].SizeIntInOut>1)
        {
            write_file<<VectorInOuts[j];
            j++;
        }
        write_file<<" B;;;"<<endl;

    }

    write_file<<"IN       vss B;;\nIN       vdd B;;"<<endl;
    write_file<<"\n\n"<<"begin\n\n"<<"-- Pattern description :"<<"\n"<<"--";


    for(i=0,j=0; i<v.size(); i++)
    {
        v[i].Acum=0;
        if(v[i].SizeIntInOut>Max)
        {

            Max=v[i].SizeIntInOut;
            NodoMax=i;

        }
        write_file<<v[i].NameInOut<<"  ";

    }


    Max=pow(2,Max);
    i=0;


    while(v[NodoMax].Acum < Max)
    {

        for(j=0; j<NumRepetition; j++)
        {

            if(firstIte)
            {
                write_file<<"\n< "<<Stime<<UTime<<">: ";
                firstIte=0;
            }
            else
            {
                write_file<<"< +"<<Itime<<UTime<<">: ";
            }

            for(k=0; k<v.size(); k++)
            {
                wxString WXincrIte=(vectorValueSignalPAT[k])->GetValue();

                if(v[k].ModeInOut=="IN" || v[k].ModeInOut=="in")
                {
                    string res=ConvertionBinary(v[k].Acum,v[k].SizeIntInOut);
                    write_file<<res<<"\t";
                }
                else
                {
                    write_file<<"?";
                    for(l=0; l<v[k].SizeIntInOut; l++)
                    {
                        write_file<<"*";
                    }
                    write_file<<"\t";
                }

                if(j==NumRepetition-1)
                {
                    int IncINT=wxAtoi(WXincrIte);
                    v[k].Acum=v[k].Acum+IncINT;
                }

            }
            write_file<<"0 1;";
            write_file<<endl;

        }

    }



    write_file<<"\nend;"<<endl;
    write_file.close();


}




void FileCreator::CreatFileIOC(char FileNameOutIOC[])
{


    unsigned int i;
    unsigned int j;
    unsigned int FlagTop=0,FlagBotton=0,FlagLeft=0,FlagRight=0,FlagIgnore=0;

    ofstream write_file(FileNameOutIOC);



    for(i=0; i<5; i++)
    {
        for(j=0; j<vectorCheck.size(); j++)
        {

            wxString res;
            res=(vectorCheck[j])->GetValue();
            string PositionInOut =string(res.mb_str());

            switch(i)
            {
            case 0:
                if(PositionInOut=="TOP")
                {

                    if(FlagTop==0)
                    {
                        FlagTop=1;
                        write_file<<"\nTOP ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,FlagTop);
                }
                break;

            case 1:
                if(FlagTop==1)
                {
                    FlagTop=0;
                    write_file<<"\n);\n\n";
                }

                if(PositionInOut=="BOTTOM")
                {

                    if(FlagBotton==0)
                    {
                        FlagBotton=1;
                        write_file<<"BOTTOM ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,FlagTop);
                }

                break;
            case 2:
                if(FlagBotton==1)
                {
                    FlagBotton=0;
                    write_file<<"\n);\n\n";
                }
                if(PositionInOut=="LEFT")
                {

                    if(FlagLeft==0)
                    {
                        FlagLeft=1;
                        write_file<<"LEFT ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,FlagBotton);
                }

                break;
            case 3:
                if(FlagLeft==1)
                {
                    FlagLeft=0;
                    write_file<<"\n);\n\n";
                }
                if(PositionInOut=="RIGHT")
                {

                    if(FlagRight==0)
                    {
                        FlagRight=1;
                        write_file<<"RIGHT ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,FlagLeft);
                }

                break;
            case 4:
                if(FlagRight==1)
                {
                    FlagRight=0;
                    write_file<<"\n);\n\n";
                }
                if(PositionInOut=="IGNORE")
                {

                    if(FlagIgnore==0)
                    {
                        FlagIgnore=1;
                        write_file<<"IGNORE ( # IOs are ordered from left to right"<<endl;
                    }
                    WriteFileIOC(&write_file,j,i,FlagRight);
                }
                break;
            }
        }
    }
    if(FlagIgnore==1)
    {
        FlagIgnore=0;
        write_file<<"\n);\n\n";
    }


    write_file.close();


}


void FileCreator::ShowInterfaceValuesFileIOC()
{
    wxPanel *Panel=new wxPanel(this);
    if(v.size()==0)
    {

        Errors=true;
        LabelIncrease=new wxStaticText(Panel,wxID_ANY,wxT("There aren't enough information\n         for create a ioc file"),wxPoint(50,200),wxDefaultSize);
        ButtonCancel=new wxButton(Panel,ID_CANCEL2, wxT("OK"),wxPoint(115,360));
        return;
    }

    Position_Values.Add(L"TOP");
    Position_Values.Add(L"BOTTOM");
    Position_Values.Add(L"LEFT");
    Position_Values.Add(L"RIGHT");
    Position_Values.Add(L"IGNORE");
    unsigned int i=0;



    LabelT=new wxStaticText(Panel,wxID_ANY,wxT("Order I/O"),wxPoint(130,10),wxDefaultSize);
    LabelPosition=new wxStaticText(Panel,wxID_ANY,wxT("Position"),wxPoint(50,60),wxDefaultSize);
    LabelName=new wxStaticText(Panel,wxID_ANY,wxT("Name"),wxPoint(150,60),wxDefaultSize);
    LabelType=new wxStaticText(Panel,wxID_ANY,wxT("Type"),wxPoint(225,60),wxDefaultSize);
    AreaValues=new wxScrolledWindow(Panel,wxID_ANY,wxPoint(35,100),wxSize(250,220),wxVSCROLL,wxT("RESULT"));
    wxBoxSizer *box=new wxBoxSizer(wxVERTICAL);
    ButtonCancel=new wxButton(Panel,ID_CANCEL2, wxT("Cancel"),wxPoint(50,340));
    ButtonOk=new wxButton(Panel,ID_OK2, wxT("Ok"),wxPoint(180,340));


    for (i=0; i<v.size(); i++)
    {
        wxBoxSizer *BoxH =new wxBoxSizer(wxHORIZONTAL);
        vectorCheck.push_back( new wxComboBox(AreaValues,wxID_ANY,wxT("TOP"),wxDefaultPosition, wxSize(110,30),Position_Values,wxCB_READONLY));
        BoxH->Add(vectorCheck.back());
        BoxH->Add(new wxStaticText(AreaValues,wxID_ANY,wxT(""),wxDefaultPosition,wxSize(30,30)));
        wxString NameWX(v[i].NameInOut.c_str(), wxConvUTF8);
        vectorInOut.push_back(new wxStaticText(AreaValues,wxID_ANY,NameWX,wxDefaultPosition,wxSize(60,30)));
        BoxH->Add(vectorInOut.back());
        wxString ModeWX(v[i].ModeInOut.c_str(), wxConvUTF8);
        vectorInOut.push_back(new wxStaticText(AreaValues,wxID_ANY,ModeWX,wxDefaultPosition,wxDefaultSize));
        BoxH->Add(vectorInOut.back());
        box->Add(BoxH);

    }


    AreaValues->SetSizer(box);
    AreaValues->FitInside();
    AreaValues->SetScrollRate(50,50);

}


void FileCreator::ShowInterfaceValuesFilePAT()
{
    wxPanel *Panel=new wxPanel(this);

    if(v.size()==0)
    {

        Errors=true;
        LabelIncrease=new wxStaticText(Panel,wxID_ANY,wxT("There aren't enough information\n         for create a pat file"),wxPoint(50,200),wxDefaultSize);
        ButtonCancel=new wxButton(Panel,ID_CANCEL2, wxT("OK"),wxPoint(115,360));
        return;
    }
    unsigned int i=0;


    wxTextValidator Num_validator(wxFILTER_NUMERIC);
    LabelTimeStart=new wxStaticText(Panel,wxID_ANY,wxT("Start Time"),wxPoint(30,10),wxDefaultSize);
    LabelTimeIncr=new wxStaticText(Panel,wxID_ANY,wxT("Increase Time"),wxPoint(30,40),wxDefaultSize);
    LabelTimeUnit=new wxStaticText(Panel,wxID_ANY,wxT("Unit Time"),wxPoint(30,70),wxDefaultSize);
    LabelRepetition=new wxStaticText(Panel,wxID_ANY,wxT("Value Repetition"),wxPoint(30,100),wxDefaultSize);
    StartTime=new wxTextCtrl(Panel,wxID_ANY,wxT("0"),wxPoint(140,5),wxSize(60,25),0,Num_validator);
    IncreTime=new wxTextCtrl(Panel,wxID_ANY,wxT("10"),wxPoint(140,35),wxSize(60,25),0,Num_validator);
    UnitTime=new wxTextCtrl(Panel,wxID_ANY,wxT("ns"),wxPoint(140,65),wxSize(60,25));
    Repetition=new wxTextCtrl(Panel,wxID_ANY,wxT("2"),wxPoint(140,95),wxSize(60,25),0,Num_validator);
    LabelIncrease=new wxStaticText(Panel,wxID_ANY,wxT("Increase"),wxPoint(30,130),wxDefaultSize);
    LabelName=new wxStaticText(Panel,wxID_ANY,wxT("Name"),wxPoint(90,130),wxDefaultSize);
    LabelType=new wxStaticText(Panel,wxID_ANY,wxT("Type"),wxPoint(155,130),wxDefaultSize);
    AreaValues=new wxScrolledWindow(Panel,wxID_ANY,wxPoint(35,160),wxSize(250,190),wxVSCROLL,wxT("RESULT"));
    wxBoxSizer *box=new wxBoxSizer(wxVERTICAL);
    ButtonCancel=new wxButton(Panel,ID_CANCEL2, wxT("Cancel"),wxPoint(50,360));
    ButtonOk=new wxButton(Panel,ID_OK2, wxT("Ok"),wxPoint(180,360));



    for (i=0; i<v.size(); i++)
    {
        wxBoxSizer *BoxH =new wxBoxSizer(wxHORIZONTAL);
        vectorValueSignalPAT.push_back(new wxTextCtrl(AreaValues,wxID_ANY,wxT("1"),wxDefaultPosition,wxSize(60,30),0,Num_validator));
        BoxH->Add(vectorValueSignalPAT.back());
        wxString NameWX(v[i].NameInOut.c_str(), wxConvUTF8);
        vectorInOut.push_back(new wxStaticText(AreaValues,wxID_ANY,NameWX,wxDefaultPosition,wxSize(60,30)));
        BoxH->Add(vectorInOut.back());
        wxString ModeWX(v[i].ModeInOut.c_str(), wxConvUTF8);
        vectorInOut.push_back(new wxStaticText(AreaValues,wxID_ANY,ModeWX,wxDefaultPosition,wxDefaultSize));
        BoxH->Add(vectorInOut.back());
        box->Add(BoxH);

    }



    AreaValues->SetSizer(box);
    AreaValues->FitInside();
    AreaValues->SetScrollRate(50,50);

}





void FileCreator::AnalyzerFile(char FileNameIn[])
{

    string Name="";
    string Mode="";


    vector <string> InOutSameType;

    char fchar;
    int FlagNewRegister=0;
    int FlagSameType=0;
    int FlagSizeVector=0;
    int State=0;

    i=0;

    int SizeInt=1;
    EqualsSize=0;

    string V1;
    string V2;

    string NameVector;


    ifstream read_file(FileNameIn);




    while(!read_file.eof())
    {
        read_file.get(fchar);

        if(State==-1)
        {
            break;
        }



        switch(fchar)
        {
        case 'A':
        case 'a':
            State=Values_States[State][0];
            break;

        case 'B':
        case 'b':
            State=Values_States[State][1];
            break;

        case 'C':
        case 'c':
            State=Values_States[State][2];
            break;

        case 'D':
        case 'd':
            State=Values_States[State][3];
            break;

        case 'E':
        case 'e':
            State=Values_States[State][4];
            break;

        case 'F':
        case 'f':
            State=Values_States[State][5];
            break;

        case 'G':
        case 'g':
            State=Values_States[State][6];
            break;

        case 'H':
        case 'h':
            State=Values_States[State][7];
            break;

        case 'I':
        case 'i':
            State=Values_States[State][8];
            break;

        case 'J':
        case 'j':
            State=Values_States[State][9];
            break;

        case 'K':
        case 'k':
            State=Values_States[State][10];
            break;

        case 'L':
        case 'l':
            State=Values_States[State][11];
            break;

        case 'M':
        case 'm':
            State=Values_States[State][12];
            break;

        case 'N':
        case 'n':
            State=Values_States[State][13];
            break;

        case 'O':
        case 'o':
            State=Values_States[State][14];
            break;

        case 'P':
        case 'p':
            State=Values_States[State][15];
            break;

        case 'Q':
        case 'q':
            State=Values_States[State][16];
            break;

        case 'R':
        case 'r':
            State=Values_States[State][17];
            break;

        case 'S':
        case 's':
            State=Values_States[State][18];
            break;

        case 'T':
        case 't':
            State=Values_States[State][19];
            break;

        case 'U':
        case 'u':
            State=Values_States[State][20];
            break;

        case 'V':
        case 'v':
            State=Values_States[State][21];
            break;

        case 'W':
        case 'w':
            State=Values_States[State][22];
            break;

        case 'X':
        case 'x':
            State=Values_States[State][23];
            break;

        case 'Y':
        case 'y':
            State=Values_States[State][24];
            break;

        case 'Z':
        case 'z':
            State=Values_States[State][25];
            break;

        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case'6':
        case '7':
        case '8':
        case '9'://26
            State=Values_States[State][26];
            break;

        case '.'://27
            State=Values_States[State][27];
            break;

        case '\n':
        case '\t':
        case ' '://28
            State=Values_States[State][28];
            break;

        case '('://29
            State=Values_States[State][29];
            break;

        case ')'://30
            State=Values_States[State][30];
            break;

        case ':'://31
            State=Values_States[State][31];
            break;

        case ';'://32
            State=Values_States[State][32];
            break;

        case '_'://33
            State=Values_States[State][33];
            break;

        case '-'://34
            State=Values_States[State][34];
            break;

        case ','://35
            State=Values_States[State][35];
            break;

        default:
            // State=State;
            break;



        }//fin de states




        switch(State)
        {
        case 0:
            break;

        case 1:
            break;

        case 2:
            break;

        case 3:
            break;


        case 4:

            break;

        case 5:

            if(fchar==',')
            {
                FlagSameType=1;
                EqualsSize=EqualsSize+1;
                InOutSameType.push_back(Name);
                Name="";
            }

            break;

        case 6:

            if(FlagNewRegister==1)
            {
                FlagNewRegister=0;
                //.................................

                if(FlagSizeVector==1)
                {
                    MakeCastingSize(&V1,&V2,&SizeInt);

                    FlagSizeVector=0;

                }//fin del if FlagSizeVector

                //..........................

                if(FlagSameType==1)
                {
                    InOutSameType.push_back(Name);
                    Name="";

                    FlagSameType=0;

                    for(i=0; i<InOutSameType.size(); i++)
                    {

                        Node.NameInOut=InOutSameType[i];
                        Node.ModeInOut=Mode;
                        Node.SizeIntInOut=SizeInt;


                        v.push_back(Node);
                    }
                    InOutSameType.erase(InOutSameType.begin(),InOutSameType.end());
                    Mode="";
                    SizeInt=1;
                    V1="";
                    V2="";

                }

                else
                {

                    Node.NameInOut=Name;
                    Node.ModeInOut=Mode;
                    Node.SizeIntInOut=SizeInt;

                    Name="";
                    Mode="";
                    SizeInt=1;

                    V1="";
                    V2="";
                    v.push_back(Node);
                    FlagNewRegister=0;
                }


            }

            Name+=fchar;
            break;

        case 7:
            break;

        case 8:
            break;

        case 9:
            Mode+=fchar;
            break;

        case 10:
            break;

        case 11:
            break;

        case 12:

            break;

        case 13:

            break;

        case 14:
            //.................................

            if(FlagSizeVector==1)
            {
                MakeCastingSize(&V1,&V2,&SizeInt);

                FlagSizeVector=0;

            }//fin del if FlagSizeVector

            Node.NameInOut=Name;
            Node.SizeIntInOut=SizeInt;
            Node.ModeInOut=Mode;


            Name="";
            Mode="";
            SizeInt=1;

            V1="";
            V2="";
            v.push_back(Node);
            break;

        case 15:
            FlagNewRegister=1;

            break;

        case 16:

            NameVector+=fchar;

            break;

        case 17:

            FlagSizeVector=1;
            NameVector+=fchar;
            V1+=fchar;
            break;

        case 18:
            NameVector+=fchar;
            break;

        case 19:
            NameVector+=fchar;
            break;

        case 20:
            NameVector+=fchar;
            break;


        case 21:
            NameVector+=fchar;
            V2+=fchar;
            break;

        case 22:
            NameVector+=fchar;
            break;

        case 23:

            NameVector+=fchar;
            for(i=0; i<=EqualsSize; i++)
            {

                VectorInOuts.push_back(NameVector);
            }
            NameVector="";
            EqualsSize=0;
            break;



        default:
            break;


        }

    }

    read_file.close();


}




void FileCreator::WriteFileIOC(ofstream *WF,int j,int i,int Flag)
{

    int BeginSize;
    BeginSize=v[j].SizeIntInOut-1;
    string NumberPosition;



    if(v[j].SizeIntInOut>1)
    {

        while(BeginSize>=0)
        {
            stringstream Midle;
            Midle<<BeginSize;
            NumberPosition=Midle.str();

            *WF<< "     IOPIN "<<v[j].NameInOut<<"("<<NumberPosition<<").0"<<endl;
            BeginSize=BeginSize-1;
        }

    }

    else
    {

        *WF<< "     IOPIN "<<v[j].NameInOut<<".0"<<endl;
    }



}


void FileCreator::OnCancel(wxCommandEvent& event)
{

    EndModal(wxID_OK);
}


void FileCreator::MakeCastingSize(string *v1,string *v2,int *SI)
{

    string vx1=*v1;
    string vx2=*v2;

    int aux1=atoi(vx1.c_str());
    int aux2=atoi(vx2.c_str());



    if(aux1>aux2)
    {
        aux1=(aux2-aux1*-1)+1;
        *SI=aux1;


    }

    else
    {
        aux1=(aux2-aux1*-1)+1;
        *SI=aux1;

    }
}



FileCreator::~FileCreator()
{
    //wxMessageBox(wxT("Destruido FileCreator"));
    //dtor
}
}





